<template>
	<view id="cart">
		<view class="" v-if="cart.length>0">
			<!-- 收货地址组件 -->
			<my-address></my-address>
			<!-- 图标 购物车 -->
			<view class="cartTit">
				<uni-icons type="shop" size="20" color="#a1a1a1"></uni-icons>
				购物车
			</view>
			
			<!-- 购物车列表 -->
			<uni-swipe-action>
				<block v-for="(goods,i) in cart" :key="i">
					<uni-swipe-action-item :right-options="options" @click="clickDel(goods)" >
						<my-goods :goods=goods 
						:showRadio="showRadio" @changeRadio="changeRadio"
						:showNumberBox="showNumberBox" @changeNumber="changeNumber"
						></my-goods>
					</uni-swipe-action-item>
				</block>
			</uni-swipe-action>
			
			<!-- 底部结算 -->
			<my-settle :allchecked="allchecked" @changeAll='changeAll' :total='total'></my-settle>
		</view>
		
		<!-- 当购物车为空时 -->
		<view class="noAnything" v-else>
			<image src="../../static/cart.jpeg" mode="widthFix"></image>
			<view class="">
				<text>快去加购吧～</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {mapState,mapMutations,mapGetters} from 'vuex'
	
	export default {
		data() {
			return {
				showRadio:true,
				showNumberBox:true,
				options:[{
				            text: '确认',
				            style: {
				                backgroundColor: '#dd524d'
				            }
				        }],
				allchecked:false,
				total:0
			};
		},
		onShow() {
			console.log('cart',this.cart)
			if(this.getCount+''>0){
				this.setBadge();
			}else{
				this.removeBadge();
			}
			
			this.isAllChecked();
			this.computTotal();
		},
		methods:{
			// 滑动后 点击删除 删除商品
			clickDel(goods){
				this.delGoods(goods);
				if(this.getCount+''>0){
					this.setBadge();
				}else{
					this.removeBadge();
				}
				
			},
			...mapMutations('m_cart',['updataGoodsState','updataGoodsCount','delGoods']),
			changeRadio(e){
				// console.log(e)
				this.updataGoodsState(e);
				// 每点击一次 遍历一次购物车数组是否被全部勾选
				this.isAllChecked()
				this.computTotal();
			},
			// 判断是否全部被勾选
			isAllChecked(){
				let arr =[]
				this.cart.forEach(v=>{
					if(v.goods_state!==true){
						arr.push(v)
					}
				})
				if(arr.length===0){
					this.allchecked = true
				}else{
					this.allchecked = false
				}
			},
			// 计算已勾选商品的总价
			computTotal(){
				let total=0;
				this.cart.forEach(v=>{
					if(v.goods_state===true){
						 total+=v.goods_price*v.goods_count
					}
				})
				this.total=total;
				console.log('total=>',total)
			},
			// 点击了全选按钮
			changeAll(){
				if(this.allchecked===true){
					this.cart.forEach(v=>{
						v.goods_state=false
						this.allchecked=false;
					})
				}else{
					this.cart.forEach(v=>{
						v.goods_state=true
						this.allchecked=true;
					})
				}
			},
			// 数字步进器发生变化时
			changeNumber(e){
				console.log(e)
				this.updataGoodsCount(e)
				this.setBadge();
				this.computTotal();
			},
			setBadge(){
				// 调用 uni.setTabBarBadge() 方法，为购物车设置右上角的徽标
				uni.setTabBarBadge({
					index:2,
					text:this.getCount+'' // 注意：text 的值必须是字符串，不能是数字
				})
			},
			removeBadge(){
				// 调用 uni.removeTabBarBadge() 方法，移除右上角的徽标
				uni.removeTabBarBadge({
					index:2,
				})
			}
		},
		computed:{
			...mapState('m_cart',['cart']),
				 // 将 m_cart 模块中的 total 映射为当前页面的计算属性
				...mapGetters('m_cart',['getCount'])
		}
	}
</script>

<style lang="scss">
	.noAnything{
		display: flex;
		height: 100vh;
		flex-direction: column;
		justify-content: space-evenly;
		align-items: center;
		text{
			text-align: center;
		}
		image{
			width: 60%;
			margin: 0 auto;
		}
	}
.cartTit{
	line-height: 70rpx;
	font-size: 28rpx;
	color: #a1a1a1;
	padding-left: 20rpx;
}
#cart{
	padding-bottom: 120rpx;
}
</style>
